package jp.sourceforge.akjrcp.videotools.dvdchapter;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUtils;

public class MPlayerDVDParser {
private String mplayerPath;
public MPlayerDVDParser(String playerPath){
	this.mplayerPath=playerPath;
}
public DVDInfomation parseDVD(File dvd){
	//log.info("parse:");
	String s=toDVDTitlesetString(dvd,1);//get first one
	//log.info(s);
	String c=getChapter(s);
	
	int titleSize=countTitleSets(s);
	String chapters[]=new String[titleSize];
	chapters[0]=c;
	for(int i=1;i<titleSize;i++){
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String s2=toDVDTitlesetString(dvd,i+1);
	//	log.info(s2);
		chapters[i]=getChapter(s2);
		
	}
	DVDInfomation dvdinfo=new DVDInfomation(dvd);
	dvdinfo.setChapters(chapters);
	return dvdinfo;
}


public String toDVDTitlesetString(File dvd,int titleset){
	String text="";
	String commands[]=toCommand(dvd, titleset);
	Akj_betasPlugin.getDefault().getProcessPrinter().printOutput(FFMpegUtils.toPrintCommand(commands)+"\n");
	Runtime r=Runtime.getRuntime();
	
	try {
		Process process=r.exec(commands);
		BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
		String tmp;
		while((tmp=reader.readLine())!=null){
			Akj_betasPlugin.getDefault().getProcessPrinter().printOutput(tmp+"\n");//log
			
			if(tmp.startsWith("=")){
				/*
				BufferedReader reader2=new BufferedReader(new InputStreamReader(process.getErrorStream()));
				String tmp2;
				while((tmp2=reader2.readLine())!=null){
					log.info(tmp2);
				}*/
				
				process.destroy();//finish here
				break;
			}else{
				text+=tmp+"\n";
			}
		}
	} catch (IOException e) {
		Akj_betasPlugin.getDefault().getProcessPrinter().printError(e.getMessage()+"\n");//log
		
		e.printStackTrace();
	}
	return text;
}
public String getChapter(String data){
	String chap="";
	String lines[]=data.split("\n");
	for (int i = 0; i < lines.length; i++) {
		if(lines[i].startsWith("CHAPTERS: ")){
			chap=lines[i].substring("CHAPTERS: ".length());
			if(chap.endsWith(",")){
				chap=chap.substring(0,chap.length()-1);//chop
			}
			break;
		}
	}
	return chap;
}
public int countTitleSets(String data){
	int r=0;
	String lines[]=data.split("\n");
	for (int i = 0; i < lines.length; i++) {
		if(lines[i].startsWith("ID_DVD_TITLES=")){
			r=Integer.parseInt(lines[i].substring("ID_DVD_TITLES=".length()));
			break;
		}
	}
	return r;
}

public String[] toCommand(File dvd,int titleset){
	List list=new ArrayList();
	list.add(mplayerPath);
	list.add("-identify");
	list.add("-dvd-device");
	list.add(dvd.getAbsolutePath());
	list.add("dvd://"+titleset);
	
	return (String[]) list.toArray(new String[list.size()]);
}
}
